﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Business;
using Dto;

namespace YaoYunDong.Controllers
{
    public class TopicsController : Controller
    {
        private readonly SportsGroupBusiness groupBusiness = new SportsGroupBusiness();
        private readonly GroupTopicBusiness topicBusiness = new GroupTopicBusiness();

        public ActionResult Details(int id, int? currentPageNo)
        {
            var topicDto = topicBusiness.GetTopic(id);
            int totalCount;
            var detailDtos = topicBusiness.GetDetails(currentPageNo ?? 1, 30, topicDto.GtId, out totalCount);
            var groupDto = groupBusiness.GetGroupById(topicDto.GtGroupId);
            ViewData["PageNo"] = currentPageNo ?? 0;
            ViewData["Topic"] = topicDto;
            var page = new PagingDto(totalCount, currentPageNo ?? 1, 30, detailDtos ?? new List<TopicDetailsDto>());
            return View("~/Views/Group/" + groupDto.GroupTemplateId + "/Details.aspx", page);
        }

        [ValidateInput(false)]
        public string SaveTopics(string title, string content, int groupId, int userId)
        {
            if (userId != 0)
            {
                var time = DateTime.Now;
                var topicDto = new GroupTopicDto
                                   {
                                       GtGroupId = groupId,
                                       GtSponsor = userId,
                                       GtTime = time,
                                       GtTitle = title
                                   };
                var detailsDto = new TopicDetailsDto
                                     {
                                         TdContent = content,
                                         TdTime = time,
                                         TdParentId = 0,
                                         TdUserId = userId
                                     };
                var newId = topicBusiness.CreateTopic(topicDto, detailsDto);
                if (newId > 0)
                {
                    return "<script>alert('发表成功！');location.href='" +
                           Url.Action("Details", "Topics", new { id = newId }) + "';</script>";
                }
                return "<script>alert('发表失败！');location.href='" + Url.Action("Topics", "Group") + "';</script>";
            }
            return "<script>alert('请登录后发帖！');location.href='" + Url.Action("Index", "Channel", new { ChannelCode = "yqzb" }) + "';</script>";
        }

        [ValidateInput(false)]
        public string Reply(string content, int topicId)
        {
            var usersDto = Session["User"] as UsersDto;
            if (usersDto != null)
            {
                var detailsDto = new TopicDetailsDto
                                     {
                                         TdContent = content,
                                         TdParentId = 0,
                                         TdTime = DateTime.Now,
                                         TdTopicId = topicId,
                                         TdUserId = usersDto.UserId,
                                     };
                var b = topicBusiness.ReplyTopic(detailsDto);
                if (b)
                {
                    return "<script>alert('发表成功！');location.href='" +
                           Url.Action("Details", "Topics", new { id = topicId }) + "';</script>";
                }
                return "<script>alert('发表失败！');location.href='" + Url.Action("Topics", "Group") + "';</script>";
            }
            return "<script>alert('请登录后发帖！');location.href='" + Url.Action("Index", "Channel", new { ChannelCode = "yqzb" }) + "';</script>";
        }

        public string ChildReply(string content, int topicId, int detailsId)
        {
            var usersDto = Session["User"] as UsersDto;
            if (usersDto != null)
            {
                var detailsDto = new TopicDetailsDto
                                     {
                                         TdContent = content,
                                         TdParentId = detailsId,
                                         TdTime = DateTime.Now,
                                         TdTopicId = topicId,
                                         TdUserId = usersDto.UserId,
                                     };
                var b = topicBusiness.ReplyTopic(detailsDto);
                return b ? "success" : "fail";
            }
            return "noLogin";
        }
    }
}
